﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using QLTT.Models;
using QLTT.Commands;
using System.Windows.Input;

namespace QLTT.ViewModels
{
    class ClassAbsenceViewModel : WorkspaceViewModel
    {
        List<AbsenceViewModel> _lstAbsenceViewModel;
        long _class;
        DateTime _date;
        icanDataContext _dataContext;
        private DelegateCommand _saveCommand;
        private DelegateCommand _cancelCommand;

        public ClassAbsenceViewModel(icanDataContext dataContext)
        {
            _lstAbsenceViewModel = new List<AbsenceViewModel>();
            _dataContext = dataContext;
            _date = DateTime.Today;
            base.DisplayName = Properties.Resources.Base_DisplayName_Absence_New;

        }
        public ClassAbsenceViewModel(icanDataContext dataContext, long classID)
        {
            _lstAbsenceViewModel = new List<AbsenceViewModel>();
            _dataContext = dataContext;
            _date = DateTime.Today;
            this.Class = classID;
            base.DisplayName = Properties.Resources.Base_DisplayName_Absence_New;

        }
        

        public ICommand SaveCommand
        {
            get
            {
                if (_saveCommand == null)
                {
                    _saveCommand = new DelegateCommand(Save);
                }
                return _saveCommand;
            }
        }
       
        public ICommand CancelCommand
        {
            get
            {
                if (_cancelCommand == null)
                {
                    _cancelCommand = new DelegateCommand(Cancel);
                }
                return _cancelCommand;
            }
        }

        private void Save()
        {
            if (_dataContext == null || _lstAbsenceViewModel == null || _lstAbsenceViewModel.Count == 0)
                return;
            foreach (AbsenceViewModel aVM in _lstAbsenceViewModel)
            {
                if (aVM.IsChecked == true && aVM.IsEnabled == true)
                    aVM.Save();
                
            }
            Mediator.NotifyColleagues<WorkspaceViewModel>(Messages.WorkspaceRemove, this);
        }
       
        private void Cancel()
        {
            Mediator.NotifyColleagues<WorkspaceViewModel>(Messages.WorkspaceRemove, this);
        }
        public DateTime Date
        {
            get { return _date; }
            set
            {
                if (_date == value)
                    return;
                _date = value;
                base.OnPropertyChanged("Date");
                this.Absences = GetAbsences();
            }
        }

        public long Class
        {
            get { return _class; }
            set
            {
                if (_class == value)
                    return;
                _class = value;
                base.OnPropertyChanged("Class");
                this.Absences = GetAbsences();
            }
        }
        public List<ClassViewModel> Classes
        {
            get
            {
                return _dataContext.Classes.Where(oClass => oClass.EndDate >= DateTime.Today).Select(oClass => new ClassViewModel(_dataContext, oClass)).ToList();
            }
        }
        public List<AbsenceViewModel> Absences
        {
            get { return _lstAbsenceViewModel; }
            set
            {
                if (_lstAbsenceViewModel == value || this.Class == 0)
                    return;
                _lstAbsenceViewModel = value;
                base.OnPropertyChanged("Absences");
            }
        }
        public List<AbsenceViewModel> GetAbsences()
        {
            List<long> lstAbsenceId = _dataContext.Absences.Where(a => a.StudentClass1.Class == this.Class && a.Date == this.Date).Select(a => a.StudentClass1.Student).ToList();
            List<AbsenceViewModel> lstAVM = _dataContext.StudentClasses.Where(st => st.Class == this.Class).Select(sc => new AbsenceViewModel(_dataContext, sc, this.Date)).ToList();
            foreach (AbsenceViewModel aVM in lstAVM)
            {
                if (lstAbsenceId.Contains(aVM.Student))
                {
                    aVM.IsEnabled = false;
                    aVM.IsChecked = true;
                }
            }
            return lstAVM;
        }

    }
}
